package com.maker.generator.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dwork.base.controller.BaseController;
import com.dwork.base.controller.entity.BaseResultObject;
import com.dwork.base.pagination.Page;
import com.dwork.base.pagination.Pagination;
import com.dwork.base.validate.groups.DEFAULT;
import com.maker.generator.entity.MetaGenAttr;
import com.maker.generator.entity.MetaGenConfig;
import com.maker.generator.entity.MetaGenObj;
import com.maker.generator.service.IGenService;

@Controller
@RequestMapping(value = "metaGenObj", name = "元对象管理")
public class MetaGenObjController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(MetaGenObjController.class);
	@Autowired
	private IGenService genService;
	
	@RequestMapping(value = "/g2manage", name = "进入元对象管理界面")
	public ModelAndView manage() {
		ModelAndView mv = getBaseMV("/metaGenObj/manage");
		return mv;
	}
	@RequestMapping(value = "/g2Edit/{id}", name = "进入元对象修改界面")
	public ModelAndView g2Edit(@PathVariable Long id) {
		ModelAndView mv = getBaseMV("/metaGenObj/editForm");
		if(id!=null){
			MetaGenObj entity = this.genService.findMetaGenObjById(id);
			mv.addObject("entity", entity);
		}
		return mv;
	}
	@RequestMapping(value = "/g2ObjAttrsManage/{id}", name = "进入元对象属性管理界面")
	public ModelAndView g2ObjAttrManage(@PathVariable Long id) {
		ModelAndView mv = getBaseMV("/metaGenObj/objAttrManage");
		if(id!=null){
			MetaGenObj entity = this.genService.findMetaGenObjById(id);
			mv.addObject("entity", entity);
		}
		return mv;
	}
	@RequestMapping(value = "/g2Add", name = "进入元对象新增界面")
	public ModelAndView g2Add() {
		ModelAndView mv = getBaseMV("/metaGenObj/addForm");
		return mv;
	}
	@RequestMapping(value = "/queryList", name = "分页查询元对象")
	@ResponseBody
	public void queryList(Integer pageNum,Integer pageSize , String objName, String objComment) {
		BaseResultObject baseResultObject = new BaseResultObject();
		Map<String, Object> paramMap = new HashMap<String,Object>();
		Page page = new Page();
		page.setCurrentPage(pageNum);
		page.setPageSize(pageSize);
		
		if(objName!=null&&!objName.equals("")){
			paramMap.put("objNameLike", objName);
		}
		if(objComment!=null&&!objComment.equals("")){
			paramMap.put("objCommentLike", objComment);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<MetaGenObj> metaGenObjPage = genService.queryMetaGenObjList(page, paramMap);
		if(metaGenObjPage!=null&&metaGenObjPage.getData()!=null){
			map.put("rows", metaGenObjPage.getData());
			map.put("pageSize", metaGenObjPage.getData().size());
			map.put("total", metaGenObjPage.getPage().getTotalCount());
		}
		baseResultObject.setData(map);
		this.putInToJsonResponse(baseResultObject);
	}
	@RequestMapping(value = "/add", name = "添加元对象")
	@ResponseBody
	public void add(@RequestBody MetaGenObj view) {
		BaseResultObject baseResultObject = new BaseResultObject();
		List<Map<String, String>> validateMessages = new ArrayList<Map<String, String>>();
		baseResultObject.setStatus(BaseController.STATUS_SUCCESS);
		baseResultObject.setMessage(BaseController.SUCCESS_MESSAGE);
		validateMessages = getValidatorMessage(view, DEFAULT.class);
		if(validateMessages!=null&&validateMessages.size()>0) {
			baseResultObject.setStatus(BaseController.STATUS_FAILURE);
			baseResultObject.setMessage(BaseController.VALIDATE_ERROR_MESSAGE);
			baseResultObject.setValidateMessages(validateMessages);
		}else{
			try{
				view = this.genService.save(view);
			}catch (Exception e) {
				logger.debug("后台错误:"+e.getMessage());
				baseResultObject.setStatus(BaseController.STATUS_FAILURE);
				baseResultObject.setMessage(BaseController.COMMON_ERROR_MESSAGE);
			}
			baseResultObject.setData(view);
		}
		putInToJsonResponse(baseResultObject);
	}
	@RequestMapping(value = "/edit", name = "编辑元对象")
	@ResponseBody
	public void edit(@RequestBody MetaGenObj view) {
		BaseResultObject baseResultObject = new BaseResultObject();
		List<Map<String, String>> validateMessages = new ArrayList<Map<String, String>>();
		baseResultObject.setStatus(BaseController.STATUS_SUCCESS);
		baseResultObject.setMessage(BaseController.SUCCESS_MESSAGE);
		MetaGenObj entity = this.genService.findMetaGenObjById(view.getId());
		entity.setObjName(view.getObjName());
		entity.setObjComment(view.getObjComment());
		entity.setObjPackageName(view.getObjPackageName());
		entity.setObjTypeName(view.getObjTypeName());
		validateMessages = getValidatorMessage(entity, DEFAULT.class);
		if(validateMessages!=null&&validateMessages.size()>0) {
			baseResultObject.setStatus(BaseController.STATUS_FAILURE);
			baseResultObject.setMessage(BaseController.VALIDATE_ERROR_MESSAGE);
			baseResultObject.setValidateMessages(validateMessages);
		}else{
			try{
				view = this.genService.update(entity);
			}catch (Exception e) {
				logger.debug("后台错误:"+e.getMessage());
				baseResultObject.setStatus(BaseController.STATUS_FAILURE);
				baseResultObject.setMessage(BaseController.COMMON_ERROR_MESSAGE);
			}
			baseResultObject.setData(view);
		}
		putInToJsonResponse(baseResultObject);
	}
	@RequestMapping(value = "/delete", name = "删除元对象")
	@ResponseBody
	public void delete(@RequestBody List<Long> ids) {
		BaseResultObject baseResultObject = new BaseResultObject();
		baseResultObject.setStatus(BaseController.STATUS_SUCCESS);
		baseResultObject.setMessage(BaseController.SUCCESS_MESSAGE);
		try{
			this.genService.deleteMetaGenObjByIds(ids);
		}catch (Exception e) {
			logger.debug("后台错误:"+e.getMessage());
			baseResultObject.setStatus(BaseController.STATUS_FAILURE);
			baseResultObject.setMessage(BaseController.COMMON_ERROR_MESSAGE);
		}	
		putInToJsonResponse(baseResultObject);
	}
	

}
